﻿using System;
using System.Collections.Generic;
using DevExpress.Web.ASPxTreeView;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web.UI;
using EntryExit.Utils;

namespace EntryExit
{
    public partial class EmployeeDetails : BasePage
    {
        public Employee SelectedEmployee { get; set; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Master is MainMaster)
            {
                ((MainMaster)this.Master).MasterTreeViewNodeSelected += EmployeeDetails_MasterTreeViewNodeSelected;
                //((MainMaster)this.Master).ClickEventHandler += EmployeeDetails_ClickEventHandler;
            }
        }

        //void EmployeeDetails_ClickEventHandler(object sender, EventArgs e)
        //{
        //    SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
        //    DisplaySelectedEmployeeDetails(SelectedEmployee);
        //}

        void EmployeeDetails_MasterTreeViewNodeSelected(object source, TreeViewNodeEventArgs e)
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            DisplaySelectedEmployeeDetails(SelectedEmployee);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
                    DisplaySelectedEmployeeDetails(SelectedEmployee);
                }

                if (SelectedEmployee != null)
                {
                    if (this.Master is MainMaster)
                    {
                        ((MainMaster)this.Master).ExpandTreeToEmployee(SelectedEmployee);
                    }
                }

            }
            catch (Exception ex)
            {
                Logger.Error("Page :EmployeeDetails.aspx.cs, Event : Page_Load", ex);
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('An internal error occured. Reload the tool and try again. If issue still persists, please report to your administrator..!');", true);
            }
        }

        
        private void DisplaySelectedEmployeeDetails(Employee employee)
        {
            lblname.Text = employee.EmployeeName;
            lbllpn.Text = employee.LPN;
            lblgpn.Text = employee.GPN;
            lblgradename.Text = employee.GradeName;
            lblJoiningDate.Text = employee.JoiningDate.ToShortDateString();
            lblemail.Text = employee.EmailAddress;
            lblcountry.Text = employee.Country;
            lblofficeName.Text = employee.OfficeName;
            lblservicelineDesc.Text = employee.ServiceLineDescription;
            lblsubservicelineDesc.Text = employee.SubServiceLineDescription;
            lblDepartmentName.Text = employee.DepartmentName;
            lblauthorityName.Text = employee.AuthorityName;

            empDetailsPanel.HeaderText = "Employee Details : " + employee.EmployeeName;
        }
    }
}